package com.cloud.bc.controller.pc;

import com.cloud.bc.controller.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.entity.*;
import com.cloud.bc.model.request.MomenRequest;
import com.cloud.bc.service.*;
import com.cloud.bc.util.JiaoYouPageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//PC首页
@AllArgsConstructor
@Controller
@RequestMapping("")
public class PcHomeController extends BaseController {
    private final BcArticleCategorysService bcArticleCategorysService;
    private final BcArticlesService bcArticlesService;
    private final BcSinglesService bcSinglesService;
    private final BcSingleMatesService bcSingleMatesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final BcSlideService bcSlideService;

    /**
     * 首页
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "51找朋友", "pc/home/index");
        //查找最新单身
        BcSingles bcSingles = new BcSingles();
        bcSingles.setIsClose(0);
        bcSingles.setAuthIdentity(1);
        IPage<BcSingles> bcSinglesIPage = bcSinglesService.selectPage(new Page(1, 20), bcSingles);
        List<BcSingles> records = bcSinglesIPage.getRecords();
        records.forEach(item -> {
            item.setAge(bcSinglesService.ageConvert(item.getBirthday()));
        });
        //脱单宝典
        IPage<BcArticles> articles = bcArticlesService.selectPage(new Page(1, 3), null);
        view.addObject("articles", articles.getRecords());
        view.addObject("slide", bcSlideService.selectAll(null));
        view.addObject("footerSingle", getFooterSingleHtml(records));
        view.addObject("bodyCss", "page-home");
        return view;
    }

    /**
     * 登录
     */
    @GetMapping("login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "登录-51找朋友", "pc/home/login");
        view.addObject("bodyCss", "page_login page_login_public");
        return view;
    }

    /**
     * 注册
     */
    @GetMapping("register")
    public ModelAndView register(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "注册-51找朋友", "pc/home/register");
        view.addObject("bodyCss", "page_login page_login_public");
        return view;
    }

    //首页底部html拼接
    private String getFooterSingleHtml(List<BcSingles> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            BcSingles bcSingles = list.get(i);
            if (i % 10 == 0) {
                String ulClass = "js-bottom-list";
                if (i == 0) {
                    ulClass = "js-top-list";
                }
                sb.append("<ul class=\"recommend-list " + ulClass + " clearfix\">");
            }
            sb.append("<li>");
            sb.append("<div class=\"user-item js-userItem\">");
            sb.append("<i class=\"user-mask\">");
            sb.append("</i>");
            sb.append("<a class=\"user-target\" href=\"/single/info/" + bcSingles.getUserId() + "\" target=\"_blank\">");
            sb.append("</a>");
            sb.append("<div class=\"user-info\">");
            sb.append("<p class=\"user-nickname _text\">");
            sb.append("<span title=\"Herges\">");
            sb.append(bcSingles.getNickname());
            sb.append("</span>");
            sb.append("</p><p>");
            sb.append(bcSingles.getAge() + "岁 " + bcSingles.getHeight() + "厘米");
            sb.append("</p></div>");
            sb.append("<img class=\"user-photo\" photo_mode =\"1\" style =\"opacity: 1;\" src =\"" + bcSingles.getPhoto() + "_200x200.jpg\">");
            sb.append("</div></li>");
            if ((i != 0 && i % 9 == 0) || (i + 1) == list.size()) {
                sb.append("</ul>");
            }
        }
        return sb.toString();
    }

}
